#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>

#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <iostream.h>
using namespace std;


#define PORT 15020//5799
//#define DEST_ADDR "150.135.152.255" //"255.255.255.255" //broadcast addr
#define CLIENT_ADDR "192.168.1.26"
#define HOST_ADDR "192.168.1.21"
#define BROADCAST_ADDR "192.168.255.255"

int main(int argc, char *argv[])
{
        int sockfd;
        char buf[256];
        struct sockaddr_in sendaddr;
        struct sockaddr_in recvaddr;
        int numbytes;
        int addr_len;
        int broadcast=1;
	char sendMSG[] ="Broadcast message from OBU!";

        if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
        {
                perror("socket");
                exit(1);
        }
       if((setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,
                                &broadcast,sizeof broadcast)) == -1)
        {
                perror("setsockopt - SO_SOCKET ");
                exit(1);
        }

        printf("Socket created\n");
 /*
        sendaddr.sin_family = AF_INET;
        sendaddr.sin_port = htons(PORT);
        sendaddr.sin_addr.s_addr = INADDR_ANY;
        memset(sendaddr.sin_zero,'\0', sizeof sendaddr.sin_zero);

 */
        recvaddr.sin_family = AF_INET;
        recvaddr.sin_port = htons(PORT);
        recvaddr.sin_addr.s_addr = INADDR_ANY; //inet_addr(CLIENT_ADDR);//INADDR_ANY

        memset(recvaddr.sin_zero,'\0',sizeof recvaddr.sin_zero);

        cout<<"Receiver Address:"<< inet_ntoa(recvaddr.sin_addr)<<endl;

        if(bind(sockfd, (struct sockaddr*) &recvaddr, sizeof recvaddr) == -1)
        {
                perror("bind");
                exit(1);
        }

        //sendaddr.sin_family = AF_INET;
        //sendaddr.sin_port = htons(PORT);
        //sendaddr.sin_addr.s_addr = inet_addr(BROADCAST_ADDR);//inet_addr(HOST_ADDR);

        //memset(sendaddr.sin_zero,'\0', sizeof sendaddr.sin_zero);


        addr_len = sizeof sendaddr;

	while (true)
    {
        if ((numbytes = recvfrom(sockfd, buf, sizeof buf, 0,
              (struct sockaddr *)&sendaddr, (socklen_t *)&addr_len)) == -1)
        {
        	perror("recvfrom");
       		exit(1);
        }

        if(numbytes>0)      printf("RSU got some message from RSE:\r\n");

        cout<<"Client Address:"<< inet_ntoa(recvaddr.sin_addr)<<"   Port:  "<<ntohs(recvaddr.sin_port)<<endl
            <<"Server  Address is:"<<inet_ntoa(sendaddr.sin_addr)<<"   Port:  "<<ntohs(sendaddr.sin_port)<<endl;

        cout<<"*******Message from RSU: "<<buf<<endl;


	//if(sendto(sockfd,sendMSG,strlen(sendMSG)+1,0,(struct sockaddr *)&sendaddr,sizeof(sendaddr)) < 0)
 //   	{
 //       	perror("SendTo");
 //      		exit(1);
 //   	}
 //   	else
 //       printf("OBE sends the broadcast message Successfully:\r\n");

 //       printf("%s \r\n",buf);
    }

        return 0;
}
